export default {
	data() {
		return {
			proId: '',
			proDetail: {},
			seckillTime: ['00', ':', '00', ':', '00'],
			spuSel: {},
			proNum: 1,
		}
	},
	async onLoad(options) {
		console.log("detail index onLoad: ", options);
		this.proId = options.id || '';
		if (this.$store.state.login.token) {
			await this.onPageInit();
		}
	},
	onShow() {
		const self = this;
		this.nowTimeInterval = setInterval(() => {
			self.getSeckillTime();
		}, 1000)
	},
	onHide() {
		clearInterval(this.nowTimeInterval);
	},
	methods: {
		// 获取初始化数据
		async onPageInit() {
			const detailRes = await this.$uniCloudCallFunction({
				name: 'xxf-product',
				action: "getProductDetail",
				data: {
					uid: this.$store.state.login.uid,
					id: this.proId
				}
			});
			console.log("detailRes: ", detailRes);
			this.proDetail = detailRes || {};
			this.spuSel = detailRes.spu[0] || {};
		},
		getSeckillTime() {
			const now = new Date();
			const endOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59); // 当天结束的时间
			const diff = endOfDay - now; // 计算剩余时间
			if (diff > 0) {
				// 计算时间
				const seconds = Math.floor(diff / 1000);
				const minutes = Math.floor(seconds / 60);
				const hours = Math.floor(minutes / 60);
				const days = Math.floor(hours / 24);

				let ss = seconds % 60;
				ss = ss <= 9 ? `0${ss}` : `${ss}`;
				let mm = minutes % 60;
				mm = mm <= 9 ? `0${mm}` : `${mm}`;
				let hh = hours % 60;
				hh = hh <= 9 ? `0${hh}` : `${hh}`;

				this.seckillTime = [hh, ':', mm, ':', ss];
			} else {
				// 如果时间已经过了当天的23:59:59，则显示0秒
				this.seckillTime = ['00', ':', '00', ':', '00'];
			}
		},
		spuChange(e) {
			this.spuSel = e || {};
		},
		proNumMinus() {
			if (this.proNum > 1) this.proNum--;
		},
		proNumPlus() {
			this.proNum++;
		},
		async addToCart() {
			const addRes = await this.$uniCloudCallFunction({
				name: 'xxf-cart',
				action: "addToCart",
				data: {
					uid: this.$store.state.login.uid,
					product_info: this.proDetail,
					spu_info: this.spuSel,
					count: this.proNum,
					status: '1'
				}
			});
			console.log("addRes: ", addRes);
			await this.$utils.showToast(this, {
				message: '已经加入购物车！',
				mode: 1
			});
		},
		forBooking() {
			const proList = [{
				cartId: '',
				productInfo: this.proDetail,
				spuInfo: this.spuSel,
				count: this.proNum,
			}];
			uni.navigateTo({
				url: `/pages/homePack/booking/index?proList=${JSON.stringify(proList)}`
			});
		}
	}
}